package com.regretbuying.front.service.categories.impl;

import com.regretbuying.front.dao.categories.CategoriesDao;
import com.regretbuying.front.service.categories.CategoriesService;
import com.regretbuying.pojo.Categories;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service("fcsi")
public class CategoriesServiceImpl implements CategoriesService {

    @Autowired
    private CategoriesDao dao;

    @Override
    public List<Categories> getFirstCategories() {
        return dao.getCategoriesListByParentID(0);
    }

    @Override
    public List<Map<Categories, List<Categories>>> getCategories() {
        List<Map<Categories, List<Categories>>> goodsCategories = new ArrayList<>() ;

        //现获取一级分类
        List<Categories> firstCategories = dao.getCategoriesListByParentID(0) ;
        for(Categories firstCate : firstCategories) {
            Map<Categories, List<Categories>> firstMap = new LinkedHashMap<>();

            //根据一级分类获取其下的二级分类
            List<Categories> secondList = dao.getCategoriesListByParentID(firstCate.getCateId()) ;


            firstMap.put(firstCate, secondList) ;
            goodsCategories.add(firstMap) ;
        }
        return goodsCategories;
    }

    @Override
    public Categories CategoriesById(int id) {
        return dao.CategoriesById(id);
    }
}
